package org.zlui65.milan

import android.graphics.Color
import android.graphics.drawable.Drawable
import android.view.View
import android.widget.TextView
import androidx.annotation.ColorInt
import androidx.annotation.ColorRes
import androidx.annotation.DimenRes
import androidx.annotation.DrawableRes
import androidx.annotation.Px
import androidx.core.content.ContextCompat

/**
 * Extension functions for styling View attributes programmatically.
 */

// Background
fun StyleBuilder.background(@DrawableRes drawableRes: Int) {
    val attrId = android.R.attr.background
    setAttribute(attrId, drawableRes)
}

fun StyleBuilder.backgroundColor(@ColorInt color: Int) {
    val attrId = android.R.attr.background
    setAttribute(attrId, color)
}

fun StyleBuilder.backgroundColorRes(@ColorRes colorRes: Int) {
    val attrId = android.R.attr.background
    setAttribute(attrId, colorRes)
}

// Padding
fun StyleBuilder.padding(@Px padding: Int) {
    val attrId = android.R.attr.padding
    setAttribute(attrId, padding)
}

fun StyleBuilder.paddingRes(@DimenRes paddingRes: Int) {
    val attrId = android.R.attr.padding
    setAttribute(attrId, paddingRes)
}

fun StyleBuilder.paddingHorizontal(@Px padding: Int) {
    val leftAttrId = android.R.attr.paddingLeft
    val rightAttrId = android.R.attr.paddingRight
    setAttribute(leftAttrId, padding)
    setAttribute(rightAttrId, padding)
}

fun StyleBuilder.paddingVertical(@Px padding: Int) {
    val topAttrId = android.R.attr.paddingTop
    val bottomAttrId = android.R.attr.paddingBottom
    setAttribute(topAttrId, padding)
    setAttribute(bottomAttrId, padding)
}

// Margin
fun StyleBuilder.margin(@Px margin: Int) {
    val attrId = android.R.attr.layout_margin
    setAttribute(attrId, margin)
}

fun StyleBuilder.marginRes(@DimenRes marginRes: Int) {
    val attrId = android.R.attr.layout_margin
    setAttribute(attrId, marginRes)
}

// Visibility
fun StyleBuilder.visibility(visibility: Int) {
    val attrId = android.R.attr.visibility
    setAttribute(attrId, visibility)
}

// Width and Height
fun StyleBuilder.width(@Px width: Int) {
    val attrId = android.R.attr.layout_width
    setAttribute(attrId, width)
}

fun StyleBuilder.height(@Px height: Int) {
    val attrId = android.R.attr.layout_height
    setAttribute(attrId, height)
}

// TextView specific extensions
fun StyleBuilder.textColor(@ColorInt color: Int) {
    val attrId = android.R.attr.textColor
    setAttribute(attrId, color)
}

fun StyleBuilder.textColorRes(@ColorRes colorRes: Int) {
    val attrId = android.R.attr.textColor
    setAttribute(attrId, colorRes)
}

fun StyleBuilder.textSize(@Px size: Float) {
    val attrId = android.R.attr.textSize
    setAttribute(attrId, size)
}

fun StyleBuilder.textSizeRes(@DimenRes sizeRes: Int) {
    val attrId = android.R.attr.textSize
    setAttribute(attrId, sizeRes)
}

fun StyleBuilder.text(text: String) {
    val attrId = android.R.attr.text
    setAttribute(attrId, text)
}

fun StyleBuilder.textStyle(style: Int) {
    val attrId = android.R.attr.textStyle
    setAttribute(attrId, style)
}

fun StyleBuilder.textAllCaps(allCaps: Boolean) {
    val attrId = android.R.attr.textAllCaps
    setAttribute(attrId, allCaps)
}

// Convenience functions for common colors
fun StyleBuilder.black() = textColor(Color.BLACK)
fun StyleBuilder.white() = textColor(Color.WHITE)
fun StyleBuilder.red() = textColor(Color.RED)
fun StyleBuilder.green() = textColor(Color.GREEN)
fun StyleBuilder.blue() = textColor(Color.BLUE)
fun StyleBuilder.gray() = textColor(Color.GRAY)

// Convenience functions for common text styles
fun StyleBuilder.bold() = textStyle(android.graphics.Typeface.BOLD)
fun StyleBuilder.italic() = textStyle(android.graphics.Typeface.ITALIC)
fun StyleBuilder.boldItalic() = textStyle(android.graphics.Typeface.BOLD_ITALIC)
fun StyleBuilder.normal() = textStyle(android.graphics.Typeface.NORMAL)
